package net.sxnic.ugr.dept.action;

/**
 * 
 * Comments: 保存部门action
 * <p>
 * Author：孙宇飞
 * <p>
 * Create Date：2010-6-4
 *  @author 吕盛槐 update date : 2011-9-25
 *  @author 张国业 update date : 2013-4-12
 * <p>
 * 
 * 
 */
import net.sxnic.ugr.UgrConstant;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.role.Role;

import org.apache.commons.lang3.StringUtils;

@SuppressWarnings("serial")
public class Save extends DeptAction {

	public void prepare() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			dept = deptManager.get(id);
			parent = dept.getParent();
		}
		if (StringUtils.isNotBlank(root)) {
			parent = deptManager.get(root);
		}
	}

	public String execute() throws Exception {

		try {
			checkRoles(Role.SYSTEM_ADMIN);

			if (parent != null)
				dept.setParent(parent);
			
			if (StringUtils.isBlank(dept.getId())) {
				
				deptManager.save(dept);
				
				parent.getChild().add(dept);
				deptManager.save(parent);
				
				msg = "添加成功";
				
			} else {
				deptManager.update(dept);
				msg = "修改成功";
			}

			// 重建deptMap
			UgrConstant.DEPT_MAP = deptManager.findMap();

			// log
			addLog("===用户===" + AppContext.getUserName(request)
					+ "===修改部门信息成功！===id===" + dept.getId());

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "right";

	}

}
